<?php

namespace App\Http\Controllers\Back;

use App\Models\Back\Code;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Schema;

/*
 * 代码生成器
 */
class CodeController extends Controller
{
    public function generate(Request $request, $type = 'table')
    {
    	if ($type == 'table') {
			if ($request->isMethod('post')) {
				//将提交的数据保存到session中
				session(['crud' => ['table' => $request->get('table'), 'title' => $request->get('title')]]);
				return redirect()->route('code.generate', ['type' => 'field']);
			} else {
				return view('back.code.crud',['type'=>'table']);
			}
	    } elseif ($type == 'field') {
		    if ($request->isMethod('post')) {
		    	$model = new Code();
				//生成代码
			    $result = $model->createCode($request->get('field'));
			    if ($result) {
				    foreach ($result as $error) {
					    echo $error . '<br>';
				    }
				    dd();
			    } else {
				    dd('控制器，模型，视图生成成功');
			    }

		    } else {
				//获取字段信息
			    $columns = Schema::getColumnListing(session('crud.table'));
			    return view('back.code.crud',['type'=>'field', 'fieldList' => $columns]);
		    }
	    }
    }
}
